<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="keywords" content="">
    <meta name="description" content="">
    <title>在线列表</title>
    <link rel="stylesheet" href="../../../component/pear/css/pear.css">
    <link rel="stylesheet" href="../../../admin/css/dimples.css">
    <link rel="icon" href="../../../admin/images/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../../../admin/css/other/center.css"/>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">用户名</label>
                <div class="layui-input-inline">
                    <input type="text" name="username" placeholder="" class="layui-input">
                </div>
                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="online-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <table id="online-table" lay-filter="online-table"></table>
    </div>
</div>
</body>

<script type="text/html" id="online-username">
    <div>
        {{d.username}}
        {{#
        if (d.current) {
        }}
        <span class="layui-badge dimples-tag-red">current</span>
        {{#
        }
        }}
    </div>
</script>

<script type="text/html" id="online-bar">
    <span {{noPerms('user:online:remove')}}>
    <span class="layui-badge-dot dimples-bg-orange"></span> 无权限
    </span>
    <a lay-event="remove" {{checkPerms('user:online:remove')}}>
    <i class="layui-icon dimples-edit-area dimples-red">&#xe7f9;</i>
    </a>
</script>

<script src="../../../component/layui/layui.js"></script>
<script src="../../../component/pear/pear.js"></script>
<script src="../../../component/pear/dimples.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'popup', 'loading', 'ajax'], function () {
        let table = layui.table;
        let form = layui.form;
        let $ = layui.jquery;
        let popup = layui.popup;
        let loading = layui.loading;
        let ajax = layui.ajax;

        let MODULE_PATH = BASE_URL + "/system/online";

        let cols = [
            [
                {type: 'checkbox'},
                {title: '会话编号', field: 'tokenId', align: 'center', width: 300},
                {title: '登陆名称', field: 'username', align: 'center', templet: '#online-username'},
                {title: '部门名称', field: 'deptName', align: 'center'},
                {title: '主机', field: 'ipaddr', align: 'center'},
                {title: '登陆地点', field: 'loginLocation', align: 'center'},
                {title: '浏览器', field: 'browser', align: 'center'},
                {title: '操作系统', field: 'os', align: 'center'},
                {title: '登陆时间', field: 'loginTime', align: 'center'},
                {title: '操作', toolbar: '#online-bar', align: 'center', width: 100}
            ]
        ]

        table.render({
            elem: '#online-table',
            url: MODULE_PATH,
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: false,
            headers: {
                Authorization: getToken()
            },
            response: {
                statusCode: 200 //规定成功的状态码，默认：0
            },
            done: function (res, curr, count) {
                //如果是直接赋值的方式，res即为：{data: [], count: 99} data为当前页数据、count为数据总长度
                if (res.code == 402) {
                    popup.permsDeny();
                }
            }
        });

        table.on('tool(online-table)', function (obj) {
            if (obj.event === 'remove') {
                window.remove(obj);
            }
        });

        form.on('submit(online-query)', function (data) {
            table.reload('online-table', {where: data.field})
            return false;
        });

        window.remove = function (obj) {
            layer.confirm('确定要踢出该用户', {icon: 3, title: '提示'}, function (index) {
                layer.close(index);
                loading.Load(5, '');
                ajax.send(MODULE_PATH + "/" + obj.data['tokenId'], {
                    type: 'delete'
                }, function (result) {
                    if (result.code == 200) {
                        popup.success(result.msg, function () {
                            obj.del();
                        });
                    } else {
                        popup.failure(result.msg);
                    }
                });
            });
        }

        window.refresh = function () {
            table.reload('online-table');
        }
    })
</script>
</html>